﻿using System;
using System.Net.NetworkInformation;
using System.Windows;
using System.Windows.Controls;

namespace Oliver.Controls
{
    public partial class OnlineOfflineControl : UserControl
    {
        public OnlineOfflineControl()
        {
            InitializeComponent();

            NetworkChange.NetworkAddressChanged += NetworkChange_NetworkAddressChanged;
            CheckNetworkStatus();
        }

        void NetworkChange_NetworkAddressChanged(object sender, EventArgs e)
        {
            CheckNetworkStatus();
        }

        private void CheckNetworkStatus()
        {
            if (NetworkInterface.GetIsNetworkAvailable())
            {
                NetworkState = NetworkState.Online;
            }
            else
            {
                NetworkState = NetworkState.Offline;
            }
        }

        private NetworkState _networkState = NetworkState.Offline;
        public NetworkState NetworkState
        {
            get { return _networkState; }
            set 
            { 
                _networkState = value;
                if (_networkState == NetworkState.Online)
                {
                    VisualStateManager.GoToState(this, "Online", true);
                }
                else
                {
                    VisualStateManager.GoToState(this, "Offline", true);
                }
            }
        }

    }

    public enum NetworkState
    {
        Online, Offline
    }

}
